home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 21 / Cream of the Crop 21 (Terry Blount) (October 1996).iso / comm / msged400.zip / src / date.c < prev    next >
C/C++ Source or Header  |  1996-07-22  |  15KB  |  615 lines

  1. /*
  2.  *  DATE.C
  3.  *
  4.  *  Written on 30-Jul-90 by jim nutt.  Changes on 10-Jul-94 by John Dennis.
  5.  *  Released to the public domain.
  6.  *
  7.  *  Parse various string date formats into a UNIX style timestamp.
  8.  */
  9.  
  10. #include "msged.h"
  11. #include "date.h"
  12. #include "strextra.h"
  13. #include "memextra.h"
  14.  
  15. static char *month[] =
  16. {
  17.     "Jan", "Feb", "Mar", "Apr", "May", "Jun",
  18.     "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
  19. };
  20.  
  21. static char *day[] =
  22. {
  23.     "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"
  24. };
  25.  
  26. static char *attr_tokens[] =
  27. {
  28.     "yms",                      /* year msg */
  29.     "yno",                      /* year now */
  30.     "mms",                      /* month msg */
  31.     "mno",                      /* month now */
  32.     "dms",                      /* day msg */
  33.     "dno",                      /* day now */
  34.     "wms",                      /* weekday msg */
  35.     "wno",                      /* weekday now */
  36.     "tnm",                      /* time msg normal */
  37.     "tnn",                      /* time now normal */
  38.     "tam",                      /* time msg atime */
  39.     "tan",                      /* time now atime */
  40.  
  41.     /* ------------- */
  42.  
  43.     "ofn",                      /* orginal from name */
  44.     "off",                      /* original from first name */
  45.     "otn",                      /* original to name */
  46.     "otf",                      /* original to first name */
  47.     "osu",                      /* original subject */
  48.     "ooa",                      /* original origin address */
  49.     "oda",                      /* orginal destination address */
  50.  
  51.     /* ------------- */
  52.  
  53.     "fna",                      /* from name */
  54.     "ffn",                      /* from first name */
  55.     "fad",                      /* from address */
  56.     "tna",                      /* to name */
  57.     "tfn",                      /* to first name */
  58.     "tad",                      /* to address */
  59.     "sub",                      /* subject */
  60.  
  61.     /* ------------- */
  62.  
  63.     "una",                      /* user name */
  64.     "ufn",                      /* user first name */
  65.     "uad",                      /* user address */
  66.     "ceh",                      /* current echo tag */
  67.     "oeh",                      /* old echo tag */
  68.  
  69.     /* ------------- */
  70.  
  71.     "ims",                      /* iso date of msg */
  72.     "ino",                      /* iso date now */
  73.     "cms",                      /* 4-digit year of msg */
  74.     "cno",                      /* 4-digit year of now */
  75.  
  76.     NULL
  77. };
  78.  
  79. static int valid_date(struct tm *tms);
  80.  
  81. time_t parsedate(char *ds)
  82. {
  83.     int t;
  84.     struct tm tm;
  85.     char work[80], *s;
  86.  
  87.     if (ds == NULL || strlen(ds) == 0)
  88.     {
  89.         return 0;
  90.     }
  91.  
  92.     memset(&tm, 0, sizeof tm);
  93.     strcpy(work, ds);
  94.  
  95.     if (strchr(ds, '-') != NULL)
  96.     {                           /* quickbbs style date */
  97.         s = strtok(work, "-");
  98.         if (s != NULL)
  99.         {
  100.             tm.tm_mon = atoi(s) - 1;
  101.         }
  102.         s = strtok(NULL, "-");
  103.         if (s != NULL)
  104.         {
  105.             tm.tm_mday = atoi(s);
  106.         }
  107.         s = strtok(NULL, " ");
  108.         if (s != NULL)
  109.         {
  110.             tm.tm_year = atoi(s);
  111.         }
  112.         s = strtok(NULL, ":");
  113.         if (s != NULL)
  114.         {
  115.             while (isspace(*s))
  116.                 s++;
  117.             tm.tm_hour = atoi(s);
  118.         }
  119.         s = strtok(NULL, " ");
  120.         if (s != NULL)
  121.         {
  122.             tm.tm_min = atoi(s);
  123.         }
  124.         tm.tm_sec = 0;
  125.     }
  126.     else
  127.     {                           /* fido style date */
  128.         s = strtok(work, " ");
  129.  
  130.         if (s == NULL)
  131.         {
  132.             return 0;
  133.         }
  134.  
  135.         if ((t = atoi(s)) == 0)
  136.         {                       /* a usenet date */
  137.             s = strtok(NULL, " ");
  138.             if (s == NULL)
  139.             {
  140.                 return 0;
  141.             }
  142.             t = atoi(s);
  143.         }
  144.         tm.tm_mday = t;
  145.         s = strtok(NULL, " ");
  146.         if (s == NULL)
  147.         {
  148.             return 0;
  149.         }
  150.         for (t = 0; t < 12; t++)
  151.         {
  152.             if (stricmp(s, month[t]) == 0)
  153.             {
  154.                 break;
  155.             }
  156.         }
  157.         if (t == 12)
  158.         {
  159.             t = 1;              /* WRA */
  160.         }
  161.         tm.tm_mon = t;
  162.         s = strtok(NULL, " ");
  163.         if (s == NULL)
  164.         {
  165.             return 0;
  166.         }
  167.         tm.tm_year = atoi(s);
  168.         s = strtok(NULL, ":");
  169.         if (s == NULL)
  170.         {
  171.             return 0;
  172.         }
  173.         while (isspace(*s))
  174.         {
  175.             s++;
  176.         }
  177.         tm.tm_hour = atoi(s);
  178.         s = strtok(NULL, ": \0");
  179.         if (s == NULL)
  180.         {
  181.             return 0;
  182.         }
  183.         tm.tm_min = atoi(s);
  184.         s = strtok(NULL, " ");
  185.         if (s != NULL)
  186.         {
  187.             tm.tm_sec = atoi(s);
  188.         }
  189.         tm.tm_isdst = -1;
  190.     }
  191. #ifdef NEVER
  192.     tm.tm_hour += 5;
  193. #endif
  194.     return mktime(&tm);
  195. }
  196.  
  197. char *itime(time_t now)
  198. {
  199.     struct tm *tm;
  200.     static char tmp[40];
  201.  
  202.     tm = localtime(&now);
  203.  
  204.     if (!tm || !valid_date((tm)))
  205.     {
  206.         sprintf(tmp, "%04d-%02d-%02d %02d:%02d:%02d",
  207.                 1970, 1, 1, 0, 0, 0);
  208.     }
  209.     else
  210.     {
  211.         sprintf(tmp, "%04d-%02d-%02d %02d:%02d:%02d",
  212.                 tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday,
  213.                 tm->tm_hour, tm->tm_min, tm->tm_sec);
  214.     }
  215.     return tmp;
  216. }
  217.  
  218. char *atime(time_t now)
  219. {
  220.     struct tm *tm;
  221.     static char tmp[40];
  222.  
  223.     tm = localtime(&now);
  224.  
  225.     if (!tm || !valid_date((tm)))
  226.     {
  227.         sprintf(tmp, "%s %s %02d %04d %02d:%02d:%02d",
  228.                 day[4], month[0], 1, 1970, 0, 0, 0);
  229.     }
  230.     else
  231.     {
  232.         sprintf(tmp, "%s %s %02d %04d %02d:%02d:%02d",
  233.                 day[tm->tm_wday], month[tm->tm_mon], tm->tm_mday,
  234.                 tm->tm_year + 1900, tm->tm_hour, tm->tm_min, tm->tm_sec);
  235.     }
  236.     return tmp;
  237. }
  238.  
  239. char *mtime(time_t now)
  240. {
  241.     struct tm *tm;
  242.     static char tmp[21];
  243.  
  244.     tm = localtime(&now);
  245.  
  246.     if (!tm || !valid_date((tm)))
  247.     {
  248.         sprintf(tmp, "%02d %s %02d  %02d:%02d:%02d",
  249.                 1, month[0], 70, 0, 0, 0);
  250.     }
  251.     else
  252.     {
  253.         sprintf(tmp, "%02d %s %02d  %02d:%02d:%02d",
  254.                 tm->tm_mday, month[tm->tm_mon], tm->tm_year,
  255.                 tm->tm_hour, tm->tm_min, tm->tm_sec);
  256.     }
  257.     return tmp;
  258. }
  259.  
  260. char *qtime(time_t now)
  261.  
  262. {
  263.     struct tm *tm;
  264.     static char tmp[20];
  265.  
  266.     tm = localtime(&now);
  267.  
  268.     if (!tm || !valid_date((tm)))
  269.     {
  270.         sprintf(tmp, "%s %02d %02d:%02d",
  271.                 month[0], 1, 0, 0);
  272.     }
  273.     else
  274.     {
  275.         sprintf(tmp, "%s %02d %02d:%02d",
  276.                 month[tm->tm_mon], tm->tm_mday, tm->tm_hour, tm->tm_min);
  277.     }
  278.     return tmp;
  279. }
  280.  
  281.  
  282. /* find_token - returns the token number or -1 if not found */
  283.  
  284. int find_token(char *token)
  285. {
  286.     int i;
  287.  
  288.     i = 0;
  289.     while (attr_tokens[i] != NULL)
  290.     {
  291.         if (stricmp(attr_tokens[i], token) == 0)
  292.         {
  293.             return i;
  294.         }
  295.         i++;
  296.     }
  297.     return -1;
  298. }
  299.  
  300.  
  301. /* Returns a pointer to the first name. Note: uses static memory. */
  302.  
  303. char *firstname(char *name)
  304. {
  305.     char *s;
  306.     static char work[40];
  307.  
  308.     memset(work, 0, sizeof work);
  309.     s = strchr(name, ' ');
  310.     if (s == NULL)
  311.     {
  312.         strcpy(work, name);
  313.     }
  314.     else
  315.     {
  316.         *s = '\0';
  317.         strcpy(work, name);
  318.         *s = ' ';
  319.     }
  320.     return work;
  321. }
  322.  
  323. /* attrib_line - builds an attribution line */
  324.  
  325. char *attrib_line(msg * m, msg * old, int olda, char *format)
  326. {
  327.     struct tm now, *tm;
  328.     char work[256], token[5], *t;
  329.     time_t n;
  330.     int num;
  331.  
  332.     if (format == NULL)
  333.     {
  334.         return NULL;
  335.     }
  336.  
  337.     memset(work, 0, sizeof work);
  338.     t = work;
  339.     n = time(NULL);
  340.     tm = localtime(&n);
  341.     now = *tm;
  342.  
  343.     if (old)
  344.     {
  345.         tm = localtime(&(old->timestamp));
  346.     }
  347.  
  348.     while (*format)
  349.     {
  350.         if (*format == '%')
  351.         {
  352.             format++;
  353.             switch (*format)
  354.             {
  355.